﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TM.Objects;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using TM.Objects.DTO;
namespace TM.DAL
{
    public  class DalManager
    {

        static string strConnectionString = string.Empty;

         public DalManager()
         {
             if (strConnectionString.Equals(string.Empty))
             {
                 strConnectionString = ConfigurationManager.ConnectionStrings["SMConnectionString"].ToString();
             }
         }

        public static  List<TMRule> GetRules(string ruleCategory)
        {
            if (strConnectionString.Equals(string.Empty))
            {
                strConnectionString = ConfigurationManager.ConnectionStrings["SMConnectionString"].ToString();
            }
            List<TMRule> rules = new List<TMRule>();

            SqlConnection con;
            SqlCommand cmd;
            try
            {

                con = new SqlConnection(strConnectionString);
                cmd = new SqlCommand("GetRules", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection.Open();
                if (ruleCategory != null && ruleCategory!=string.Empty)
                {
                    cmd.Parameters.Add("@RuleType", SqlDbType.Int).Value = ruleCategory.ToUpper() == "OPTION" ? 2 : 1;
                }
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    TMRule rule = new TMRule();

                    rule.RuleID = Convert.ToInt32(dr["RuleID"]);
                    rule.RuleName = Convert.ToString(dr["RuleName"]);
                    rule.RuleDescription = Convert.ToString(dr["RuleDescription"]);
                    rule.RuleType = ruleCategory;
                    rule.RuleXml = Convert.ToString(dr["RuleXml"]);
                    rule.RuleText = Convert.ToString(dr["RuleText"]);
                    rule.ExecutionOrder = Convert.ToInt32(dr["ExecutionOrder"]);
                    rules.Add(rule);


                }
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                rules = null;         
                throw ex;
            }
            finally
            {
                    
                con = null;
                cmd = null;
            }

            return rules;
        }

        public static List<TMStockEnt> GetStockSymbols()
        {
            if (strConnectionString.Equals(string.Empty))
            {
                strConnectionString = ConfigurationManager.ConnectionStrings["SMConnectionString"].ToString();
            }
            List<TMStockEnt> symbols = new List<TMStockEnt>();

            SqlConnection con;
            SqlCommand cmd;
            try
            {

                con = new SqlConnection(strConnectionString);
                cmd = new SqlCommand("GetStockSymbols", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection.Open();
                
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    TMStockEnt symbol = new TMStockEnt();

                    symbol.StockID = Convert.ToInt32(dr["StockID"]);
                    symbol.Symbol = Convert.ToString(dr["StockSymbol"]);

                    symbols.Add(symbol);



                }
                //TMStockEnt symbol = new TMStockEnt();
                //symbol.StockID = 1;
                //symbol.Symbol = "IBM";
                //symbols.Add(symbol);

                //symbol = new TMStockEnt();
                //symbol.StockID = 2;
                //symbol.Symbol = "GOOG";
                //symbols.Add(symbol);


                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                symbols = null;
                throw ex;
            }
            finally
            {

                con = null;
                cmd = null;
            }

            return symbols;
        }

        public static bool InsertRule(TMRule rule)
        {

            if (strConnectionString.Equals(string.Empty))
            {
                strConnectionString = ConfigurationManager.ConnectionStrings["SMConnectionString"].ToString();
            }

            try
            {
               

                SqlConnection con = new SqlConnection(strConnectionString);
                SqlCommand cmd = new SqlCommand("InsertRule", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection.Open();

                cmd.Parameters.Add("@RuleName", SqlDbType.VarChar).Value = rule.RuleName;
                cmd.Parameters.Add("@RuleType", SqlDbType.Int).Value = rule.RuleType == "Stock" ? 1 : 2;
                cmd.Parameters.Add("@RuleXml", SqlDbType.VarChar).Value = rule.RuleXml;
                cmd.Parameters.Add("@RuleText", SqlDbType.VarChar).Value = rule.RuleText;
                cmd.Parameters.Add("@RuleDescription", SqlDbType.VarChar).Value = rule.RuleDescription;
                cmd.Parameters.Add("@ExecutionOrder", SqlDbType.Int).Value = rule.ExecutionOrder;



                cmd.ExecuteNonQuery();

                cmd.Connection.Close();
                cmd = null;
                con = null;

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;
        }

        public static bool InsertLog(string source, string message)
        {

            if (strConnectionString.Equals(string.Empty))
            {
                strConnectionString = ConfigurationManager.ConnectionStrings["SMConnectionString"].ToString();
            }

            try
            {


                SqlConnection con = new SqlConnection(strConnectionString);
                SqlCommand cmd = new SqlCommand("InsertLog", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection.Open();

                cmd.Parameters.Add("@Source", SqlDbType.VarChar).Value = source;
                cmd.Parameters.Add("@LogCode", SqlDbType.VarChar).Value = "Log";
                cmd.Parameters.Add("@LogMessage", SqlDbType.VarChar).Value = message;
              



                cmd.ExecuteNonQuery();

                cmd.Connection.Close();
                cmd = null;
                con = null;

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;
        }

        public static List<string> GetLogs(string source)
        {
            if (strConnectionString.Equals(string.Empty))
            {
                strConnectionString = ConfigurationManager.ConnectionStrings["SMConnectionString"].ToString();
            }
            List<string> messages = new List<string>();
            messages.Add("test");
            return messages;
        }

        public static List<TMStockEnt> GetRuleResults(DateTime CreatedDate, int EntityType, int? RuleId) //0=stock;1=option
        {
            if (strConnectionString.Equals(string.Empty))
            {
                strConnectionString = ConfigurationManager.ConnectionStrings["SMConnectionString"].ToString();
            }
            List<TMStockEnt> results = new List<TMStockEnt>();
            SqlConnection con;
            SqlCommand cmd;
            try
            {

                con = new SqlConnection(strConnectionString);
                cmd = new SqlCommand("GetRuleResult", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection.Open();
                cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = CreatedDate.Equals(DateTime.MinValue) ? DateTime.Now : CreatedDate;
                cmd.Parameters.Add("@entityType", SqlDbType.Int).Value = EntityType;
                if (RuleId != null)
                {
                    cmd.Parameters.Add("@ruleId", SqlDbType.Int).Value = RuleId;
                }
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                   // TMRuleResult result = new TMRuleResult();
                    TMStockEnt StockInfo = new TMStockEnt();
                    string XmlResult = Convert.ToString(dr["ResultXml"]);
                    StockInfo =  Utility.DeserializeFromXml<TMStockEnt>(XmlResult);


                    StockInfo.RuleID = Convert.ToInt32(dr["RuleID"]);
                    StockInfo.RuleName = Convert.ToString(dr["RuleName"]);
                    StockInfo.RuleDescription = Convert.ToString(dr["RuleDescription"]);
                    StockInfo.Company = Convert.ToString(dr["CompanyName"]); ;
                    StockInfo.CreatedDate = Convert.ToDateTime(dr["CreatedDate"]);
                    StockInfo.EntityType = Convert.ToInt16(dr["EntityType"]);
                    StockInfo.StockID = Convert.ToInt32(dr["StockID"]);
                    StockInfo.Symbol = Convert.ToString(dr["StockSymbol"]);
                    StockInfo.XmlResult = Convert.ToString(dr["ResultXml"]);


                    results.Add(StockInfo);


                }
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                results = null;
                throw ex;
            }
            finally
            {

                con = null;
                cmd = null;
            }

            return results;
        }
    }



}

